package com.example.pdfsearch;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.pdfsearch.activity.PDFSearchActivity;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Button btnUpload;
    private ImageView ivBack;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        requestPermission();
    }

    private void initViews() {
        btnUpload = findViewById(R.id.tv_upload);
        ivBack = findViewById(R.id.iv_back);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("PDFSearch");
        ivBack.setVisibility(View.GONE);
        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,PDFSearchActivity.class));
            }
        });
    }

    @SuppressLint("InlinedApi")
    private void requestPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
        .subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) {
                if (permission.granted) {
                    // 用户已经同意该权限
                    Log.d(TAG, permission.name + " is granted.");
                } else if (permission.shouldShowRequestPermissionRationale) {
                    // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                    Log.d(TAG, permission.name + " is denied. More info should be provided.");
                    Toast.makeText(MainActivity.this,"您已拒绝了权限",Toast.LENGTH_SHORT).show();
                } else {
                    // 用户拒绝了该权限，并且选中『不再询问』
                    Log.d(TAG, permission.name + " is denied.");
                    Toast.makeText(MainActivity.this,"您已拒绝了权限",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
